import re
import requests
class Guba(object):
    def __init__(self,url,filename):
        self.url=url
        self.headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43'}
        self.filename=filename
        self.write_txt=open(f'{self.filename}.txt','w',encoding='utf8')
    def __del__(self):
        self.write_txt.close()
    def download(self):
        #下载原始页面
        response=requests.get(url=self.url,headers=self.headers)
        with open(f'{self.filename}.html','wb') as f:
            f.write(response.content)
    def parse_data(self):
        with open(f'{self.filename}.html','r',encoding='utf8') as f:
            html=f.read()
            #获取ul
            ul = re.findall('<ul class="newlist" tracker-eventcode="gb_xgbsy_ lbqy_rmlbdj">(.*?)</ul>',html,re.S)[0]
            #print(res)
            # 获取li
            li_list=re.findall('<li>(.*?)</li>',ul,re.S)
            #print(li_list)
            #写标题
            self.write_txt.writelines(['阅读量\t','评论量\t','标题\t','作者\t','日期\n'])
            for li in li_list:
                #阅读量，评论量
                read_count,comment_count=re.findall('<cite>(.*?)</cite>',li,re.S)
                #print(read_count.strip(),'====',comment_count.strip())
                #标题
                title=re.findall('<span class="sub">.*?<a .*? class="note">(.*?)</a></span>',li,re.S)[0]
                #作者
                author=re.findall('<cite class="aut">.*?<font>(.*?)</font>.*?</cite>',li,re.S)[0]
                #日期
                date=re.findall('<cite class="date">(.*?)</cite>',li,re.S)[0]
                s = '\t'.join([read_count.strip(),comment_count.strip(),title,author,date])
                #将内容写入到文件中
                self.write_txt.write(s+"\n")
def main():
    url = 'http://guba.eastmoney.com/default,99_1.html'
    guba=Guba(url,'guba')
    #guba.download()
    #解析数据
    guba.parse_data()
if __name__=='__main__':
    main()

